package com.pactera.madp.cp.controller.oms.employee;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pactera.madp.admin.api.dto.WxEmpDto;
import com.pactera.madp.admin.api.vo.WxEmpVo;
import com.pactera.madp.common.core.util.R;
import com.pactera.madp.common.log.annotation.SysLog;
import com.pactera.madp.common.security.annotation.Inner;
import com.pactera.madp.cp.api.dto.emp.EmpIndexDTO;
import com.pactera.madp.cp.api.dto.emp.EmployeeQueryDTO;
import com.pactera.madp.cp.api.em.permission.ReqPerEnum;
import com.pactera.madp.cp.api.entity.WorkEmployeeEntity;
import com.pactera.madp.cp.handler.CorpContextHolder;
import com.pactera.madp.cp.job.sync.WorkEmpServiceSyncLogic;
import com.pactera.madp.cp.service.IWorkEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.List;

/**
 * @author: yangpengwei
 * @time: 2020/12/4 2:39 下午
 * @description 企业成员
 */
@RestController
@AllArgsConstructor
@RequestMapping("/workemployee")
@Api(value = "workemployee", tags = "企业成员")
public class EmployeeController {

    private IWorkEmployeeService employeeService;
    private WorkEmpServiceSyncLogic workEmpServiceSyncLogic;

    /**
     * 成员 - 同步企业成员
     */
    @SysLog("同步企业成员")
    @PutMapping("/synemployee")
    @ApiOperation(value = "同步企业成员")
    public R synEmployee() {
        int corpId = CorpContextHolder.getCorpId();
        workEmpServiceSyncLogic.onSyncWxEmp(corpId);
        return R.ok();
    }

    /**
     * 成员 - 成员列表
     */
    @GetMapping("/page")
    @ApiOperation(value = "查询成员列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name="permission",value="权限:ALL-全部,DEPARTMENT-部门,EMPLOYEE-员工")
    })
    public R index(EmpIndexDTO req, ReqPerEnum permission, Page page) {
        return employeeService.index(req, permission,page);
    }

    /**
     * 成员 - 成员列表搜索条件数据
     */
    @GetMapping("/searchcondition")
    @ApiOperation(value = "成员列表搜索条件数据")
    public R searchCondition() {
        return R.ok(employeeService.searchCondition());
    }

    /**
     * 成员 - 成员列表
     */
    @GetMapping("/selectAgentEmployee")
    @ApiOperation(value = "查询成员列表")
    public R selectAgentEmployee(String employeeName) {
        List<WorkEmployeeEntity> workEmployeeEntities = employeeService.selectAgentEmployee(employeeName);
        return R.ok(workEmployeeEntities);
    }

    @SysLog("样例导出")
    @PostMapping("/exit-excel")
    @ApiOperation(value = "样例导出")
    public void exitExcel(HttpServletResponse response, OutputStream out){
        employeeService.exitExcel(response,out);
    }

    @SysLog("Excel导入")
    @PostMapping("/input-excel")
    @ApiOperation(value = "Excel导入")
    public R inputExcel(@RequestParam("file") MultipartFile file ){
     return employeeService.inputExcel(file);
    }

    @SysLog("Excel错误信息列表导出")
    @PostMapping("/excel-error-exit")
    @ApiOperation(value = "Excel错误信息列表导出")
    public void excelErrorMsg(HttpServletResponse response, OutputStream out){
        employeeService.excelErrorMsg(response, out);
    }

    @Inner
    @GetMapping("/check/{mobile}")
    @ApiOperation(value = "查询成员")
    public R<WxEmpVo> getEmp(@PathVariable("mobile") String mobile) {
        return employeeService.getEmp(mobile);
    }

    @Inner
    @PostMapping
    @ApiOperation(value = "新增成员")
    public R<Boolean> addEmployee(@RequestBody WxEmpDto dto) {
        return employeeService.addEmployee(dto);
    }

    @Inner
    @PutMapping
    @ApiOperation(value = "更新成员")
    public R<Boolean> updateEmployee(@RequestBody WxEmpDto dto) {
        return employeeService.updateEmployee(dto);
    }

    @Inner
    @DeleteMapping("/{wxUserId}")
    @ApiOperation(value = "删除成员")
    public R<Boolean> deleteEmployee(@PathVariable("wxUserId") String wxUserId) {
        return employeeService.deleteEmployee(wxUserId);
    }

    @PostMapping("/list-ids")
    @ApiOperation(value = "根据deptIds(sys_dept.dept_id)查询成员id列表")
    public R<List<Integer>> getEmployeeIdsByDeptIds(@RequestBody EmployeeQueryDTO dto) {
        return R.ok(employeeService.getEmployeeIdsByDeptIds(dto.getDeptIds()));
    }

}
